<?php
namespace app\admin\controller;

use think\controller;
use think\Request;
use app\admin\model\Permission as PermissionModel;

class Permission extends Base
{
    protected $model;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $this->model = new PermissionModel();
    }

    public function index()
    {
        $data = $this->model->field('id,pid,name,method,ctime,status')->select();

        $data = \obj_tree($data);

        $this->assign("data", $data);
        return $this->fetch();
    }

    public function add()
    {
        if (request()->isPost()) {
            $param = input('post.');

            if ($param['role_name'] == '') {
                $this->error("角色名不能为空");
            }

            $powers = '';
            
            if (!empty($param['check'])) {
                $power = $param['check'];
                $powers = implode(',', $power);
            }

            try {
                $this->model->save([
                    'name' => $param['role_name'],
                    'permission' => $powers,
                    'description' => $param['description'],
                    'create_time' => time()
                ]);
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误提示:".$msg);
            }
            $this->success('添加成功', 'index');
        }

        $data_permission = model('permission')->field('id, pid, name')->where('status', 1)->select();

        $data_permission = obj_tree($data_permission);

        $this->assign('data_permission', $data_permission);
        return $this->fetch();
    }

    public function edit($id)
    {
        $data = $this->model->find($id);

        if (request()->isPost()) {
            $param = input('post.');

            if ($param['role_name'] == '') {
                $this->error("角色名不能为空");
            }

            $powers = '';
            
            if (!empty($param['check'])) {
                $power = $param['check'];
                $powers = implode(',', $power);
            }

            try {
                $this->model->save([
                    'name' => $param['role_name'],
                    'permission' => $powers,
                    'description' => $param['description'],
                    'create_time' => time()
                ], ['id' => $id]);
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误提示:".$msg);
            }
            $this->success(config('msg.1003'), 'index');
        }

        $data_permission = model('permission')->field('id, pid, name')->where('status', 1)->select();

        $data_permission = obj_tree($data_permission);

        // 已有权限数组
        $role_powers = explode(',', $data->permission);

        $this->assign('role_powers', $role_powers);

        $this->assign('data_permission', $data_permission);
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function del($id = null)
    {
        if (Request::instance()->isAjax()) {
            if ($this->model->where('pid', 'in', $id)->value('id')) {
                return ['code'=>0, 'msg'=>'此权限子权限不为空, 若要删除请先清空子权限'];
            }

            if ($this->model->destroy($id)) {
                return ['code' => 2,'msg'=>config('msg.1003')];
            } else {
                return ['code' => 0,'msg'=>config('msg.1004')];
            }
        }
    }

    /**
     * 权限状态调整
     *
     * @param integer $id
     * @param integer $status
     * @return void
     */
    public function duty($id, $status = 0)
    {
        if (Request::instance()->isAjax()) {
            if ($this->model->save(['status'=>$status], ['id' => $id])) {
                return ['code'=>2, 'msg'=>config('msg.1003')];
            } else {
                return ['code'=>0,'msg'=>config('msg.1004')];
            }
        }
    }
}
